package cn.forkway.learn.jdbcTry;

import java.sql.*;
import java.util.Collection;
import java.util.Map;

/**
 * @program: java-learn-jdbc
 * @className: demo04.java
 * @author: River Ray
 * @description: 基本的增删改查
 * @createTime 2021年10月08日 19:29:00
 */
public class demo04 {
   public static void main(String[] args) {
      Connection conn = null;
      PreparedStatement ps = null;
      ResultSet rs = null;

      try {
         Class.forName("com.mysql.cj.jdbc.Driver");
         String URL = "jdbc:mysql://localhost:3306/jdbctest?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
         String USER = "root";
         String PWD = "250032";
          conn = DriverManager.getConnection(URL, USER, PWD);
//         关闭事务自动提交
         conn.setAutoCommit(false);

         String sql = "insert into t_user(loginName, loginPwd, realName) values(?,?,?)";
         ps = conn.prepareStatement(sql);
         ps.setString(1,"lzp");
         ps.setString(2 ,"21222");
         ps.setString(3 ,"leizhenPeng");
         int count = ps.executeUpdate();
         System.out.println(count); //虽然count有数数目，但是由于手动提交事务，所以不作数

         //手动提交事务 不行就 coon.rollback()
         conn.commit();
      } catch (ClassNotFoundException | SQLException e) {
         e.printStackTrace();
      } finally {
         if (rs != null) {
            try {
               rs.close();
            } catch (SQLException throwables) {
               throwables.printStackTrace();
            }
         }
         if (ps != null) {
            try {
               ps.close();
            } catch (SQLException throwables) {
               throwables.printStackTrace();
            }
         }
         if (conn != null) {
            try {
               conn.close();
            } catch (SQLException throwables) {
               throwables.printStackTrace();
            }
         }
      }

   }


}
